﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.Global;
using CE.AreaCountry;

public partial class Admin_area_country_AddNewArea : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }

        if (!Page.IsPostBack)
        {

            if (Request.Params["ArId"] != null)
            {
                Int64 _arid;
                if (Int64.TryParse(Request.Params["ARId"].ToString(), out _arid))
                {

                    ShowAreaDetails(_arid);
                }
            }
            else
                clearProcess();
        }

    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        clearProcess();
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewArea.aspx");
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            AreaInfo _areainfo = new AreaInfo();
            AreaInfoDB _areainfodb = new AreaInfoDB();

            _areainfo.AreaId = Convert.ToInt64(lblArId.Text.Trim());
            _areainfo.Description = txtAreaRegion.Text.Trim();
            _areainfo.Status = Convert.ToBoolean(chkAreaStatus.Checked.ToString());

            _areainfo.AreaId = _areainfodb.Insert(_areainfo);

            if (_areainfo.AreaId > 0)
            {
                clearProcess();
                lblMessage.Text = "Saved successfully!";
            }
            else
            {
                lblError.Text = "Saving failed!";
            }
        }
    }

    public void clearProcess()
    {
        lblArId.Text = "0";
        txtAreaRegion.Text = "";

        btnSubmit.Text = "Submit";
        btnClear.Text = "Clear";
    }

    private void ShowAreaDetails(Int64 ArId)
    {
        AreaInfo _areainfo = new AreaInfo();
        AreaInfoDB _areainfodb = new AreaInfoDB();
        _areainfo = _areainfodb.GetAreaInfo(ArId);

        if (_areainfo != null)
        {
            lblArId.Text = _areainfo.AreaId.ToString();
            txtAreaRegion.Text = _areainfo.Description.ToString();
            chkAreaStatus.Checked = Convert.ToBoolean(_areainfo.Status);

            btnSubmit.Text = "Update";
            btnClear.Text = "Add New";

        }

    }
}
